package com.lkj.youmeng.adapter;

import android.content.Context;
import android.graphics.Bitmap;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.target.SimpleTarget;
import com.bumptech.glide.request.transition.Transition;
import com.lkj.youmeng.R;
import com.lkj.youmeng.data.FriendData;

import java.util.ArrayList;

public class CityAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder>{
    private Context mContext;
    private ArrayList<FriendData> mList = new ArrayList<>();
    private final int HOR_RECYCLER = 0;
    private final int VER_RECYCLER = 1;

    public CityAdapter(Context mContext, ArrayList<FriendData> mList) {
        this.mContext = mContext;
        this.mList = mList;
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view;
        if (viewType == HOR_RECYCLER){
            view = LayoutInflater.from(mContext).inflate(R.layout.city_item, null);
            return new HorViewHolder(view);
        }else {
            view = LayoutInflater.from(mContext).inflate(R.layout.friend_item, null);
            return new VerViewHolder(view);
        }
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        if (holder instanceof HorViewHolder){
            HorViewHolder horViewHolder = (HorViewHolder) holder;
            LinearLayoutManager layoutManager = new LinearLayoutManager(mContext);
            layoutManager.setOrientation(RecyclerView.HORIZONTAL);
            horViewHolder.recyclerView.setLayoutManager(layoutManager);
            horViewHolder.recyclerView.setAdapter(new UserListAdapter(mContext,mList));
        }else if (holder instanceof VerViewHolder){
            final VerViewHolder verViewHolder = (VerViewHolder) holder;
            Glide.with(mContext).asBitmap().load(mList.get(position-1).getPic()).into(new SimpleTarget<Bitmap>() {
                @Override
                public void onResourceReady(@NonNull Bitmap resource, @Nullable Transition<? super Bitmap> transition) {
                    verViewHolder.publishPic.setImageBitmap(resource);
                }
            });
            verViewHolder.name.setText(mList.get(position-1).getName());
            verViewHolder.msg.setText(mList.get(position-1).getMsg());
        }
    }

    @Override
    public int getItemCount() {
        return mList.size()+1;
    }

    class HorViewHolder extends RecyclerView.ViewHolder{
        private RecyclerView recyclerView;
        public HorViewHolder(@NonNull View itemView) {
            super(itemView);
            recyclerView = itemView.findViewById(R.id.item_recycler);
        }
    }

    class VerViewHolder extends RecyclerView.ViewHolder{
        private ImageView publishPic;
        private TextView name,msg;
        public VerViewHolder(@NonNull View itemView) {
            super(itemView);
            publishPic = itemView.findViewById(R.id.publish_pic);
            name = itemView.findViewById(R.id.item_name);
            msg = itemView.findViewById(R.id.item_msg);
        }
    }

    @Override
    public int getItemViewType(int position) {
        if (position == 0){
            return HOR_RECYCLER;
        }else {
            return VER_RECYCLER;
        }
    }
}
